package com.erp.erp_servers.CustomerComplaint.imp;

import com.erp.erp_dao.CustomerComplaint.ICustomerComplaintDAO;
import com.erp.erp_dao.CustomerComplaint.ICustomerComplaintFileDAO;
import com.erp.erp_dao.CustomerComplaint.ICustomerComplaintProductDAO;
import com.erp.erp_entitys.CustomerComplaint.*;
import com.erp.erp_servers.CustomerComplaint.ICustomerComplaintService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ImpCustomerComplaintService implements ICustomerComplaintService {
    @Autowired
    private ICustomerComplaintDAO complaintDAO;
    @Autowired
    private ICustomerComplaintProductDAO productDAO;
    @Autowired
    private ICustomerComplaintFileDAO fileDAO;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        CustomerComplaintEntity complaint = complaintDAO.selectInfoById(id);
        if(complaint==null){return 0;}
        if(!CollectionUtils.isEmpty(complaint.getProductList())){
            List<Integer> idList = new ArrayList<Integer>();
            for (CustomerComplaintProductEntity dbProduct : complaint.getProductList()){
                idList.add(dbProduct.getId());
            }
            if (idList.size()>0){
                productDAO.deleteByIdList(idList);
            }
        }
        if(!CollectionUtils.isEmpty(complaint.getFileList())){
            List<Integer> idList = new ArrayList<Integer>();
            for (CustomerComplaintFileEntity dbFile : complaint.getFileList()){
                idList.add(dbFile.getId());
            }
            if (idList.size()>0){
                fileDAO.deleteByIdList(idList);
            }
        }
        return complaintDAO.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(CustomerComplaintEntity record) {
        return complaintDAO.insert(record);
    }

    @Override
    public int insertSelective(CustomerComplaintEntity record) {
        return complaintDAO.insertSelective(record);
    }

    @Override
    public CustomerComplaintEntity selectByPrimaryKey(Integer id) {
        return complaintDAO.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(CustomerComplaintEntity record) {
        return complaintDAO.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(CustomerComplaintEntity record) {
        return complaintDAO.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo<CustomerComplaintEntity> selectCustomerComplainPage(CustomerComplaintVo ccVo) {
        List<CustomerComplaintEntity> customerComplaintList = complaintDAO.selectCustomerComplainList(ccVo);
        //获取分页信息
        PageInfo pageInfo = new PageInfo(customerComplaintList);
        if(CollectionUtils.isEmpty(customerComplaintList)){
            pageInfo.setList(null);
            return pageInfo;
        }
        pageInfo.setList(customerComplaintList);
        return pageInfo;
    }

    @Override
    public int updateBoxStatusBykey(CustomerComplaintVo ccVo){
        return complaintDAO.updateBoxStatusBykey(ccVo);
    }

    @Override
    public CustomerComplaintEntity selectInfoById(Integer id){
        return complaintDAO.selectInfoById(id);
    }

    @Override
    public int insertProAndFile(CustomerComplaintEntity record) {
        if(record==null){return 0;}
        int num = complaintDAO.insert(record);
        if(num>0){
            List<CustomerComplaintProductEntity> productList = record.getProductList();
            if (!CollectionUtils.isEmpty(productList)){
                for(CustomerComplaintProductEntity pro : productList){
                    pro.setCustomerComplaintId(record.getId());
                    productDAO.insert(pro);
                }
            }
            List<CustomerComplaintFileEntity> fileList = record.getFileList();
            if (!CollectionUtils.isEmpty(fileList)){
                for(CustomerComplaintFileEntity file : fileList){
                    file.setCustomerComplaintId(record.getId());
                    file.setFileExecutor(record.getExecutor());
                    String fileName = file.getFileName();
                    if(!StringUtils.isEmpty(fileName)){
                        if (StringUtils.isEmpty(file.getFileType())){
                            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                            file.setFileType(suffix);
                        }
                    }
                    file.setFileUploadTime(new Date(System.currentTimeMillis()));
                    fileDAO.insert(file);
                }
            }
        }
        return num;
    }

    @Override
    public int updateInfoById(CustomerComplaintEntity record){
        if (record==null || null == record.getId() || "".equals(record.getId())){
            return 0;
        }
        int num = complaintDAO.updateByPrimaryKey(record);
        if(num>0){
            List<CustomerComplaintProductEntity> productList = record.getProductList();
            List<CustomerComplaintProductEntity> dbProductList = productDAO.selectProductByCid(record.getId());
            if (!CollectionUtils.isEmpty(dbProductList)){
                List<Integer> idList = new ArrayList<Integer>();
                for (CustomerComplaintProductEntity dbProduct : dbProductList){
                    idList.add(dbProduct.getId());
                }
                if (idList.size()>0){
                    productDAO.deleteByIdList(idList);
                }
            }
            if (!CollectionUtils.isEmpty(productList)){
                for (CustomerComplaintProductEntity pro : productList){
                    pro.setCustomerComplaintId(record.getId());
                    productDAO.insert(pro);
                }
            }
            List<CustomerComplaintFileEntity> fileList = record.getFileList();
            List<CustomerComplaintFileEntity> dbFileList = fileDAO.selectFileByCid(record.getId());
            if (!CollectionUtils.isEmpty(dbFileList)){
                List<Integer> idList = new ArrayList<Integer>();
                for (CustomerComplaintFileEntity dbFile : dbFileList){
                    idList.add(dbFile.getId());
                }
                if (idList.size()>0){
                    fileDAO.deleteByIdList(idList);
                }
            }
            if (!CollectionUtils.isEmpty(fileList)){
                for (CustomerComplaintFileEntity file : fileList){
                    file.setCustomerComplaintId(record.getId());
                    file.setFileExecutor(record.getExecutor());
                    String fileName = file.getFileName();
                    if(!StringUtils.isEmpty(fileName)){
                        if (StringUtils.isEmpty(file.getFileType())){
                            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                            file.setFileType(suffix);
                        }
                    }
                    file.setFileUploadTime(new Date(System.currentTimeMillis()));
                    fileDAO.insert(file);
                }
            }
        }
        return num;
    }

    @Override
    public boolean isExist(String oddNumber) {
        List<CustomerComplaintEntity> existList = complaintDAO.isExist(oddNumber);
        return CollectionUtils.isEmpty(existList)?false:true;
    }

    @Override
    public int getCountNum(){
        return complaintDAO.getCountNum();
    }

    @Override
    public List<CustomerComplaintAndProduct> selectCustomerComplainAndProductList(CustomerComplaintVo ccVo){
        List<CustomerComplaintAndProduct> customerComplaintList = complaintDAO.selectCustomerComplainAndProductList(ccVo);
        return customerComplaintList;
    }

}
